package cn.kgc.kgde.bookstore.controller;

import cn.kgc.kgde.bookstore.entity.Admin;
import cn.kgc.kgde.bookstore.entity.ResultInfo;
import cn.kgc.kgde.bookstore.service.AdminService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author myt
 * @since 2022-05-23
 */
@RestController
public class AdminController {
    @Autowired
    private AdminService adminService;

    @PostMapping("/login")
    public ResultInfo login(String userCode, String userPassword, HttpSession session){
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        Admin admin2 = new Admin();
        if (userCode!=null && userCode!=""){
            admin2.setUserCode( userCode );
        }
        if (userPassword!=null && userCode!=""){
            admin2.setUserPassword( userPassword );
        }
        queryWrapper.select("userCode","userPassword")
                .eq("userCode",admin2.getUserCode())
                .eq("userPassword",admin2.getUserPassword());
        Admin admin=this.adminService.getOne(queryWrapper);
        System.out.println("admin = " + admin);
        if(admin!=null){
            session.setAttribute("admin",admin);
            return new ResultInfo(200,"登录成功",admin);
        }
        return new ResultInfo(500,"登录失败");
    }
    @GetMapping("/logout")
    public ResultInfo logout(HttpSession session){
        session.invalidate();
        return new ResultInfo(200,"已退出");
    }


    //测试
    @GetMapping("/list")
    public ResultInfo list(){
        List<Admin> adminList=this.adminService.list(null);
        return new ResultInfo(200,"查询成功",adminList);
    }
}
